package com.ehome.plugin

import com.android.build.api.artifact.SingleArtifact
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.android.build.gradle.AppPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.register

class LegacyTaskBridging : Plugin<Project> {
    private val TAG =  LegacyTaskBridging::class.simpleName
    override fun apply(project: Project) {
        println("apply $TAG ...")

        project.plugins.withType(AppPlugin::class.java) {
            val androidComponents = project.extensions.getByType(ApplicationAndroidComponentsExtension::class.java)
            androidComponents.onVariants { variant ->
                variant.sources.assets?.let {
                    val propertyBaseCopyTaskProvider = project.tasks.register<PropertyBasedCopy>("create${variant.name}Asset")
                    println("$TAG register task ${variant.name}...")
                    propertyBaseCopyTaskProvider.configure {
                        task:PropertyBasedCopy ->
                        task.from("src/common")
                        task.include("**/*asset*.*")
                    }

                    it.addGeneratedSourceDirectory(
                        propertyBaseCopyTaskProvider,
                        PropertyBasedCopy::outputDirectory
                    )
                }

                project.tasks.register<VerifyAssetTask>("${variant.name}VerifyAsset"){
                    output.set(project.layout.buildDirectory.dir("intermediates/recipe/$it."))
                    assets.set(variant.artifacts.get(SingleArtifact.ASSETS))
                }
            }
        }
    }
}